package com.plum.admin.modules.message.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plum.admin.common.util.R;
import com.plum.admin.modules.message.entity.SysMessage;
import com.plum.admin.modules.message.service.ISysMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.plum.admin.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2021-09-24
 */
@RestController
@RequestMapping("/sys/message")
@Api(tags = "消息模板")
public class SysMessageController extends BaseController {
    @Autowired
    private ISysMessageService messageService;
    @ApiOperation("查询消息模板列表")
    @GetMapping
    @PreAuthorize("hasAnyAuthority('sys:message:list')")
    public R<Object> list(Integer page, Integer pageSize, SysMessage message){
        return R.ok(messageService.page(new Page<>(page,pageSize),
                Wrappers.<SysMessage>lambdaQuery()
                .eq(ObjectUtil.isNotNull(message.getMessageType()),
                        SysMessage::getMessageType,message.getMessageType())));
    }
    @PostMapping
    @ApiOperation("保存消息模板")
    @PreAuthorize("hasAnyAuthority('sys:message:insert')")
    public R<Object> save(@RequestBody SysMessage message){
        return R.ok(messageService.sendMessage(message));
    }
    @PutMapping
    @ApiOperation("修改消息模板")
    @PreAuthorize("hasAnyAuthority('sys:message:update')")
    public R<Object> update(@RequestBody SysMessage message){
        return R.ok(messageService.updateMessage(message));
    }

}
